package com.kingint.web.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.common.unitedresult.K;
import com.kingint.mbg.sys.entity.Notice;
import com.kingint.mbg.sys.entity.NoticeUser;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.mbg.sys.mapper.NoticeMapper;
import com.kingint.web.sys.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.sys.service.NoticeUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-12-28
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeUserService noticeUserService;

    @Override
    public Object getListneed(UmsMember currentMember) {
        Long UmsId = currentMember.getId();
        Integer hotelId = currentMember.getHotelId();
        //获取公告
        List<Notice> notices = baseMapper.getListneed(UmsId.longValue(), hotelId);

        return K.ok().data(notices).message("展示成功");
    }

    @Override
    public Object reads(UmsMember currentMember, Long id) {
        QueryWrapper<NoticeUser> queryWrapper = new QueryWrapper<>();
        Notice byId = this.getById(id);
        if (byId == null) {
            return K.error().message("没有查找到此公告");
        }
        queryWrapper.eq("user_id", currentMember.getId()).eq("notice_id", id);
        NoticeUser one = noticeUserService.getOne(queryWrapper);
        int count = 0;
        if (one == null) {//说明没被读过
            NoticeUser noticeUser = new NoticeUser();
            noticeUser.setCreateTime(new Date())
                    .setNoticeId(id)
                    .setNoticeTitle(byId.getNoticeTitle())
                    .setUserId(currentMember.getId())
                    .setUsername(currentMember.getUsername())
                    .setReadFlag(1)//表示为已读
                    .setLastTime(new Date()).setCreateTime(new Date());
            count++;//浏览量加1
            boolean save = noticeUserService.save(noticeUser);
        } else {//不等于空说明该公告已被该用户读过
            long time = one.getLastTime().getTime();//上次查看的时间戳
            long millis = System.currentTimeMillis();//获取当前查看时间
            if (millis - time > (1000 * 60 * 60 * 24)) {//上次查看时间大于一天
                count++;
            }
            one.setLastTime(new Date());
            noticeUserService.updateById(one);
        }
        Integer issue = byId.getNoticePageview();//浏览量
        if (issue == null) {
            issue = count;
        } else {
            issue = issue + count;
        }
        byId.setNoticePageview(issue);
        this.updateById(byId);

        return K.ok().message("阅读成功");
    }

    @Override
    public Object notread(UmsMember currentMember) {
        Integer count = baseMapper.notreads(currentMember);//获取未读个数
        return K.ok().data(count);
    }
}
